package com.lgCore.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

@Controller
@RequestMapping("/api/user")
public class UserController {
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public String add(int threadNum,int executeNum) {
        try {

            Date date = new Date();
            ExecutorService executorService = Executors.newCachedThreadPool();
            List<FutureTask> futureTaskList = new ArrayList<>();

            for(int j=0;j<threadNum;j++){
                futureTaskList.add((FutureTask) executorService.submit(()->{
                    for(int i=0;i<executeNum;i++){
                        //发送数据
                        try {
                            ClientBootstrap.send();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }));

            }

//            //并发处理
//            for(int i=0;i<num;i++){
//                TaskCallable taskCallable = new TaskCallable();
//                FutureTaskScheduler.add(taskCallable);
//            }
//            while(true){
//                if(FutureTaskScheduler.isTask()){
//                    Thread.sleep(20L);
//                }else{
//                    break;
//                }
//
//            }
            futureTaskList.forEach(s -> {
                try {
                    s.get();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            });
//            System.out.println("时间：" + (new Date().getTime() - date.getTime()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "22222";
    }

}
